<!DOCTYPE html>
<html lang="en">
<title>私教课程</title>
<#import "/layout/layout.html" as body/>
<@body.page>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title overflow">
                    <h5 class="pull-left">私教课程列表</h5>
                    <a href="to_add_course_sercet" class="btn btn-sm btn-primary pull-right">添加新私教</a>
                </div>
                <div class="ibox-content">
                    <div class="table-responsive">
                        <form class="form-inline" role="form">
                            <div class="form-group">
                                <label for="coachId">课程ID：</label>
                                <input class="form-control" type="number" min="1" id="id" name="id"/>
                            </div>
                            <div class="form-group" style="display: none;">
                                <label for="coachId">课程ID：</label>
                                <input class="form-control" type="number" min="1" id="xxx" name="xxx"/>
                            </div>
                            <div class="form-group">
                                <label for="coachId">教练：</label>
                                <select class="form-control" id="coachId" name="coachId">
                                    <option value="-1">全部</option>
                                    <#if coachList??>
                                        <#list coachList as coach>
                                            <option value="${coach.id!}">${coach.nickName!}</option>
                                        </#list>
                                    </#if>
                                </select>
                            </div>
                            <div class="form-group margin-left">
                                <label>状态：</label>
                                <select class="form-control" id="status" name="status">
                                    <option value="-1">全部</option>
                                    <option value="2">售卖中</option>
                                    <option value="3">已下架</option>
                                </select>
                            </div>
                            <button type="button" class="btn btn-outline btn-primary" style="margin-top: 5px" onclick="course_sercet_list(1)">确认</button>
                        </form>
                        <table class="table table-hover table-center dataTables-example">
                            <thead>
                            <tr>
                                <th>课程ID</th>
                                <th>课程名称</th>
                                <th>教练</th>
                                <th>普通价格</th>
                                <th>会员价格</th>
                                <th>最少起卖节数</th>
                                <th>上课门店</th>
                                <th>已报</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="course_sercet_list">

                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="ibox-footer overflow">
                    <h4 class="pull-left">合计：<span id="courseSercetCount">0</span>个课程</h4>
                    <div id="copot-page" class="pull-right"></div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    //TEST
    $(document).ready(function () {
        course_sercet_list(1);

        $(document).keypress(function (e) {
            // 回车键事件
            if (e.which === 13) {
                debugger
                course_sercet_list(1);
            }
        });
    });

    function course_sercet_list(currentIndex) {
        $("#course_sercet_list").empty();
//    debugger;
        var coachId = $("#coachId").val();
        var status = $("#status").val();
        var courseId = $("#id").val();

        var data = {
            "coachId": coachId,
            "status": status,
            "id": courseId
        };
        $.ajax({
            url: "course_sercet_list?pageIndex=" + currentIndex + "&pageSize=" + 10,
            type: "POST",
            data: data,
            success: function (result) {

//                    var htm="<option value='-1'>全部</option>";
//                    $.each(result.coach_info_list,function (i,re) {
//                        debugger;
//                        htm+="<option value='"+re.id+"'>"+re.nickName+"</option>";
//                    })  ;
//                    $("#coachName").html(htm);

                var html = "";
                //分页
                new Page({
                    parent: $("#copot-page"),
                    nowPage: currentIndex,
                    pageSize: 10,
                    totalCount: result.course_sercet_count,
                    setting: {
                        defaultPage: 7,//默认显示可点击分页的个数
                        firstPageText: "首页",
                        prevPageText: "上一页",
                        nextPageText: "下一页",
                        lastPageText: "尾页"
                    },
                    callback: function (nowPage, totalPage) {
                        course_sercet_list(nowPage);
                    }
                });

                $("#courseSercetCount").text(result.course_sercet_count);
                $.each(result.course_sercet_list, function (i, result) {

                    html += "<tr><td>" + result.id + "</td>";
                    html += "<td>" + result.title + "</td>";
                    html += "<td>" + result.coachName + "</td>";
                    html += "<td>" + result.servicePrice.primePrice + "</td>";
                    html += "<td>" + result.servicePrice.price + "</td>";
                    html += "<td>" + result.minCount + "</td>";
                    html += "<td>" + result.storeNames + "</td>";
                    html += "<td>" + result.currentCount + "</td>";
                    html += "<td>" + result.status + "</td>";

                    html += "<td>";
                    if (result.status == '售卖中') {
                        html += "<button type='button' onclick='closeCourse(" + result.id + ")' class='btn btn-default'><i class='fa fa-arrow-circle-down'></i> 下架</button>";
                    }
                    if (result.status == '已下架') {
                        //maxxiang 先把删除功能隐藏 html += "<button type='button' onclick='deleteCourse("+result.id+")' class='btn btn-default'><i class='fa fa-trash-o'></i> 删除</button>";
                    }
                    html += "<button type='button' onclick='editCourse(" + result.id + ")' class='btn btn-default'><i class='fa fa-pencil'></i> 编辑</button>";
                    html += "</td>";
                    html += "</tr>";

                });
                $("#course_sercet_list").html(html);

            }
        });
    }

    //查看
    function detailCourse(id) {
        window.location = "to_course_camp_detail?id=" + id;
    }

    //跳转编辑
    function editCourse(id) {
        window.location = "to_course_secret_edit?id=" + id;
    }

    //下架
    function closeCourse(id) {
//    debugger
        swal({
            title: "确定要下架吗?",
            text: "点击下架后该私教课程将立即变为下架状态!",
            type: "warning",
            showCancelButton: true,
            cancelButtonText: "容我三思！",
            confirmButtonText: "下架!",
        }).then(function () {
            $.ajax({
                type: "post",
                url: "course_close",
                data: {"id": id},
                success: function (result) {
                    if (result) {
                        swal("下架成功!", "页面即将重新加载,请稍后...", "success");
                        window.setTimeout(function () {
                            window.location.reload()
                        }, 1000);
                    }
                }
            });
        });
    }

    function deleteCourse(courseId) {

        swal({
            title: "确定要删除吗?",
            text: "删除后不可恢复!",
            type: "warning",
            showCancelButton: true,
            cancelButtonText: "容我三思！",
            confirmButtonText: "删除!"
        }).then(function () {
            $.ajax({
                type: "post",
                url: "delete_course_secret",
                data: {"courseId": courseId},
                success: function (result) {
                    if (result) {
                        swal("删除成功!", "页面即将重新加载,请稍后...", "success");
                        window.setTimeout(function () {
                            window.location.reload()
                        }, 1000);
                    }
                }
            });
        });
    }

</script>

</@body.page>
</html>